package com.itheima.mall.controller.pmsController;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.R;
import com.itheima.mall.service.pmsService.PmsProductCategoryService;
import com.itheima.pojo.pms.PmsProductCategory;
import com.itheima.vo.pms.PmsProductCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/productCategory")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService productCategoryService;

     //添加商品分类
     @PostMapping("/create")
     public R create(@RequestBody PmsProductCategory pmsProductCategory){
         productCategoryService.save(pmsProductCategory);
         return R.ok("添加成功");
        }

     //删除商品分类
     @PostMapping("/delete/{id}")
     public R delete(@PathVariable("id") Long id){
         productCategoryService.removeById(id);
         return R.ok("删除成功");
     }

     //修改导航栏显示状态
     @PostMapping("update/navStatus")
     public R updateNavStatus(@RequestParam List<Long> ids, int navStatus){
         List<PmsProductCategory> categoryList = productCategoryService.selectByIds(ids);
         for (PmsProductCategory pmsProductCategory : categoryList) {
             pmsProductCategory.setNavStatus(navStatus);
         }
         productCategoryService.updateById(categoryList);
         return R.ok(ids.size());
     }


     //修改显示状态
     @PostMapping("update/showStatus")
     public R updateShowStatus(@RequestParam List<Long> ids,int showStatus){
         List<PmsProductCategory> categoryList = productCategoryService.selectByIds(ids);
         for (PmsProductCategory pmsProductCategory : categoryList) {
             pmsProductCategory.setShowStatus(showStatus);
         }
         productCategoryService.updateById(categoryList);
         return R.ok(ids.size());
     }


     //修改商品分类
     @PostMapping("/update/{id}")
     public R update(@RequestBody PmsProductCategory pmsProductCategory){
         productCategoryService.update(pmsProductCategory);
         return R.ok("修改成功");
     }


     //根据id获取商品分类
     @GetMapping("/{id}")
     public R oneList(@PathVariable("id") Long id){
         PmsProductCategory category = productCategoryService.selectByid(id);
         return R.ok(category);
     }


        //查询所有一级分类及子分类
        @GetMapping("/list/withChildren")
        public R withChildren(){
            List<PmsProductCategoryVo> withChildrenList = new ArrayList<>();
            // 条件构造器 一级分类 leave = 0
            LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PmsProductCategory::getLevel,0);
            // 一级分类数据
            List<PmsProductCategory> leave1 = productCategoryService.withChildren(wrapper);
            for (PmsProductCategory pmsProductCategory : leave1) {
                // 创建 WithChildren 类
                PmsProductCategoryVo withChildren = new PmsProductCategoryVo();
                // 条件构造器 查询子级
                LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
                lqw.eq(PmsProductCategory::getParentId,pmsProductCategory.getId());
                List<PmsProductCategory> leave2 = productCategoryService.withChildren(lqw);
                // 将一级分类 和 二级分类 装配到 WithChildren 类
                BeanUtils.copyProperties(pmsProductCategory,withChildren);
                withChildren.setChildren(leave2);
                // 添加到 WithChildren类 集合
                withChildrenList.add(withChildren);
            }
            return R.ok(withChildrenList);
        }

    //分页查询商品分类
    @GetMapping("/list/{parentId}")
    public R listParentId(@PathVariable("parentId") Long parentId,int pageNum,int pageSize){
        // 构建分页构造器
        Page<PmsProductCategory> pageInfo = new Page<>(pageNum,pageSize);
        // 构造条件构造器
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getParentId,parentId);
        // 分页查询
        Page<PmsProductCategory> page = productCategoryService.page(pageInfo,wrapper);
        // 获取 records 数据
        List<PmsProductCategory> records = page.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum); // 当前页
        map.put("pageSize",pageSize); // 每页总数
        map.put("total",page.getTotal()); // 总数据
        map.put("totalPage",page.getPages()); // 总页数
        map.put("list",records);
        return R.ok(map);
    }

}

